package org.ajc.org.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import org.ajc.org.dto.RoleDTO;
import org.ajc.base.service.impl.BaseServiceImpl;
import org.ajc.org.domain.Employee;
import org.ajc.org.mapper.EmployeeMapper;
import org.ajc.org.query.EmployeeQuery;
import org.ajc.org.service.EmployeeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Objects;


@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> deptTree() {
        return employeeMapper.deptTree();
    }

    @Override
    public List<String> queryPermissionsById(Long id) {
        return employeeMapper.queryPermissionsById(id);
    }

    @Override
    public Object upload(MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setNeedVerify(true);//开启校验
//        System.out.println(file.getSize());
        //importExcelMore()相比于importExcel()
        ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore(file.getInputStream(), Employee.class, importParams);

        System.out.println("=============保存的数据=============");
        result.getList().forEach(System.out::println);
        employeeMapper.batchInsert(result.getList());
        System.out.println("=============错误的数据=============");
        result.getFailList().forEach(System.out::println);
        return result.getFailList();
    }

    @Override
    public void download(List<Employee> employees, ByteArrayOutputStream outputStream) throws Exception {
        Workbook workbook = null;
        try {
            workbook = ExcelExportUtil.exportExcel(new ExportParams(), Employee.class, employees);
            workbook.write(outputStream);
        } finally {
            try {
                Objects.requireNonNull(workbook).close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Employee> selectByKey(EmployeeQuery empQuery) {
        return employeeMapper.selectByKey(empQuery);
    }

    @Override
    public List<String> queryRoleByEmpId(Long id) {
        return employeeMapper.queryRoleByEmpId(id);
    }

    @Transactional
    @Override
    public void setRole(RoleDTO roleDTO) {
        employeeMapper.deleteRoleById(roleDTO.getEmployee().getId());
        employeeMapper.setRole(roleDTO);
    }
}
